package cn.liumouren.jzxd.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.liumouren.jzxd.dao.SceneManagerApplyDao;
import cn.liumouren.jzxd.entity.Scene;
import cn.liumouren.jzxd.entity.SceneManager;
import cn.liumouren.jzxd.entity.SceneManagerApply;
import cn.liumouren.jzxd.service.SceneManagerApplyService;
import cn.liumouren.jzxd.service.SceneManagerService;
import cn.liumouren.jzxd.service.SceneService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

/**
 * 景点管理员申请表(Scenemanagerapply)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 15:55:39
 */
@Service("sceneManagerApplyService")
public class SceneManagerApplyServiceImpl extends ServiceImpl<SceneManagerApplyDao, SceneManagerApply> implements SceneManagerApplyService {

    @Autowired
    private SceneService sceneService;
    @Autowired
    private SceneManagerService sceneManagerService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SceneManagerApply scenemanagerapply) {
        scenemanagerapply.setApplyTime(new Date());
        scenemanagerapply.setState(0);
        return !StrUtil.isBlank(scenemanagerapply.getUserId()) && super.save(scenemanagerapply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SceneManagerApply scenemanagerapply) {
        if (scenemanagerapply.getState() == 1) {
            // 申请成功
            // 1.判断该用户是否为景点管理员
            SceneManager sm = sceneManagerService.getOne(new LambdaQueryWrapper<SceneManager>().eq(SceneManager::getUserId, scenemanagerapply.getUserId()));
            if (sm != null) {
                // 已经是景点管理员
                return false;
            }
            // 2.添加一个新的景点
            Scene scene = new Scene();
            scene.setContactName(scenemanagerapply.getName());
            scene.setPhone(scenemanagerapply.getPhone());
            scene.setName(scenemanagerapply.getManageSceneName());
            scene.setAddress(scenemanagerapply.getManageSceneAddress());
            scene.setDescription(scenemanagerapply.getManageSceneDescription());
            scene.setHot(60);
            sceneService.save(scene);
            // 3.成为该景点的sceneManager
            SceneManager sceneManager = new SceneManager();
            sceneManager.setUserId(scenemanagerapply.getUserId());
            sceneManager.setSceneId(scene.getId());
            sceneManager.setAddTime(new Date());
            sceneManagerService.save(sceneManager);
        }
        // 修改申请状态
        return super.updateById(scenemanagerapply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }
}